package xyz.xpman.prisonerfitness.activity;


import android.app.Activity;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import xyz.xpman.prisonerfitness.R;
import xyz.xpman.prisonerfitness.service.GroupRestTimeUtil;
import xyz.xpman.prisonerfitness.util.ScreenUtil;
import xyz.xpman.prisonerfitness.util.ToastUtil;

@RequiresApi(api = Build.VERSION_CODES.O)
public class GroupRestTimeSettingActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_rest_time_setting);

        initPopWindow();
        initView();
    }

    private void initPopWindow() {
        // 设置窗口宽度，高度则自适应
        Point windowsSize = ScreenUtil.getWindowsSize(this);
        int width = (int)((float) windowsSize.x * 0.85);
        getWindow().setLayout(width, LinearLayout.LayoutParams.WRAP_CONTENT);
    }


    private void initView() {
        SeekBar seekBar = findViewById(R.id.seekBar);
        TextView secondView = findViewById(R.id.second);

        // 设置休息时间为之前保存的时间
        int groupRestTime = GroupRestTimeUtil.get();
        secondView.setText(String.valueOf(groupRestTime));


        // 取消：关闭弹窗
        findViewById(R.id.cancel).setOnClickListener(view-> finish());


        // 确定：保存时间后关闭弹窗
        findViewById(R.id.confirm).setOnClickListener(view-> {
            int progress = seekBar.getProgress();
            if (GroupRestTimeUtil.save(progress)) {
                finish();
            } else {
                ToastUtil.showText(this, "保存失败！");
            }
        });


        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            boolean isScreen = false;

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                secondView.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                isScreen = true;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                isScreen = false;
            }
        });

    }

}